package cn.limeizi.shoppingcar;

import java.sql.*;

/**
 * @Author: 李美姿
 * @Date: 2019/7/19 15:25
 * @Version 1.0
 * @描述：
 */
public class MyTools {

    /*
     * 数据库连接
     * */
    public static Connection getConn() {
        Connection connection = null;
        try {
            /*
             * 1.导包
             * 2.加载数据库驱动
             * */
            Class.forName("com.mysql.jdbc.Driver");
            //3.获得数据库连接
            String url = "jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8";
            //?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
            String user = "root";
            String password = "";
            connection = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;

    }

    /*
     * 关闭数据库
     * */
    public static void closeMysql(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {

        /*
         * 关闭ResultSet
         * */
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        /*
         * 关闭PreparedStatement
         * */
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        /*
         * 关闭Connection
         * */
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
